package com.chengxiaoxiao.service.impl;

import com.chengxiaoxiao.commons.Page;
import com.chengxiaoxiao.dao.BookDao;
import com.chengxiaoxiao.dao.impl.BookDaoImpl;
import com.chengxiaoxiao.domain.Books;
import com.chengxiaoxiao.service.BookService;

import java.util.List;

public class BookServiceImpl implements BookService {

    private BookDao dao = new BookDaoImpl();

    @Override
    public void add(Books books) {
        dao.add(books);
    }

    @Override
    public void update(Books books) {
        dao.update(books);
    }

    @Override
    public void delete(long id) {
        dao.delete(id);
    }

    @Override
    public List get() {
        return null;
    }

    @Override
    public Books getbyId(long id) {

        return dao.getbyId(id);
    }

    @Override
    public Page getListPage(String num) {

        int pageNum = 1;
        if (null != num && num != "") {
            pageNum = Integer.parseInt(num);
        }

        int total = dao.getTotalRecordsNum();
        Page page = new Page(pageNum, total);
        List<Books> list = dao.getListPage(page.getStartIndex(), page.getPageSize());

        page.setRecords(list);

        return page;
    }

    @Override
    public Page getListPageByBookName(String num,String bookName) {
        int pageNum = 1;
        if (null != num && !"".equalsIgnoreCase(num)) {
            pageNum = Integer.parseInt(num);
        }

        int totalNumber = dao.getTotalRecordsNumByBookName(bookName);

        Page page = new Page(pageNum, totalNumber);
        List list = dao.getPageLikeSNum(page.getStartIndex(), page.getPageSize(),bookName);
        page.setRecords(list);

        return page;
    }

    @Override
    public List<Books> getBooksByCategory(String id) {

        return dao.getBooksByCategory(id);
    }

    @Override
    public Page getBooksByCategoryPage(String id,String num) {

        int pageNum = 1;
        if (null != num && !"".equalsIgnoreCase(num)) {
            pageNum = Integer.parseInt(num);
        }

        int totalNumber = dao.getBooksByCategoryPageNumber(id);

        Page page = new Page(pageNum, totalNumber);
        List list = dao.getBooksByCategoryPage(page.getStartIndex(), page.getPageSize(),id);
        page.setRecords(list);

        return page;

    }

    @Override
    public Books getBookbyIsbn(String isbn) {
        return dao.getBookByIsbn(isbn);
    }
}
